<!-- eslint-disable vue/no-use-v-if-with-v-for -->
<!-- eslint-disable vue/valid-v-slot -->
<template>
  <div class="main-container">
    <div style="background: var(--color-bg-2); margin-bottom: 20px; border-radius: 10px">
      <a-tabs type="line" v-model="form.type" auto-switch @change="tabClick">
        <a-tab-pane title="个人实名" key="0" />
        <a-tab-pane title="个体实名" key="1" />
        <a-tab-pane title="企业实名" key="2" />
      </a-tabs>
    </div>
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="serchForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="name" hide-label>
                    <a-input v-model="form.name" placeholder="请输入姓名" allow-clear />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="phone" hide-label>
                    <a-input v-model="form.phone" placeholder="请输入手机号" allow-clear />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="identityCard" hide-label>
                    <a-input v-model="form.identityCard" placeholder="请输入身份证号" allow-clear />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="userId" hide-label>
                    <a-select
                      v-model="form.userId"
                      placeholder="请选择客户"
                      allow-clear
                      allow-search
                      :options="userLists"
                      :virtual-list-props="{ height: 200 }"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="state" hide-label>
                    <a-select v-model="form.state" placeholder="请选择实名状态" allow-clear>
                      <a-option :value="-1">未实名</a-option>
                      <a-option :value="0">已实名</a-option>
                      <a-option :value="1">待审核</a-option>
                      <a-option :value="2">已失败</a-option>
                      <a-option :value="3">不通过</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="mode" hide-label>
                    <a-select v-model="form.mode" placeholder="请选择实名方式" allow-clear>
                      <a-option :value="0">验证码</a-option>
                      <a-option :value="1">国政通</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      v-model="form.addTime"
                      style="width: 100%"
                      show-time
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                      placeholder="请选择开始实名时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      v-model="form.endTime"
                      style="width: 100%"
                      show-time
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                      placeholder="请选择结束实名时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15">
                      <a-button
                        type="primary"
                        v-permission="'/app/user/real/name/get/list'"
                        html-type="submit"
                        @click="doRefresh(1)"
                        ><template #icon>
                          <icon-search />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>搜索</template></a-button
                      >
                      <a-button v-permission="'/app/user/real/name/get/list'" @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button
            type="outline"
            v-permission="'/app/user/real/name/export'"
            @click="exportLists"
            :loading="loading"
          >
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :bordered="{ wrapper: true, cell: bordered }"
          :stripe="(striped as boolean)"
          :loading="tableLoading"
          :data="dataList"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          :scrollbar="true"
          :scroll="{ x: 1000 }"
          table-layout-fixed
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :width="item.width"
              :ellipsis="true"
              :tooltip="true"
              :title="(item.title as string)"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
            >
              <template v-if="item.key === 'state'" #cell="{ record }">
                <a-tag v-if="record.user.realName == undefined">未知 </a-tag>
                <a-tag color="green" v-else-if="record.user.realName == 0">已实名 </a-tag>
                <a-tag color="blue" v-else-if="record.user.realName == 1">待审核 </a-tag>
                <a-tag color="red" v-else-if="record.user.realName == 2">已失败 </a-tag>
              </template>
              <template v-if="item.key === 'mode'" #cell="{ record }">
                <span v-if="record.mode == 0">验证码 </span>
                <span v-else-if="record.mode == 1">国政通 </span>
              </template>
              <template v-else-if="item.key === 'userName'" #cell="{ record }">
                {{ record.user.userName }}
              </template>
              <template v-else-if="item.key === 'actions'" #cell="{ record }">
                <a-space :size="2">
                  <a-button
                    v-permission="'/app/user/real/name/clear'"
                    style="padding: 0px 4px"
                    type="text"
                    size="small"
                    @click="clearReal(record)"
                    >清除</a-button
                  >
                  <a-button
                    v-permission="'/app/user/real/name/auditing'"
                    style="padding: 0px 4px"
                    type="text"
                    size="small"
                    @click="Real(record)"
                    >审核</a-button
                  >
                </a-space>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
    <a-modal
      :mask-closable="false"
      :before-close="cancel"
      v-model:visible="showV"
      title="审核实名"
      width="500px"
      title-align="start"
    >
      <a-form ref="showRef" :model="detail">
        <a-form-item field="success" label="审核验证">
          <a-radio-group v-model="detail.success">
            <a-radio :value="true">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div> </div
                  ><div className="custom-radio-card-title">通过</div>
                </div>
              </template>
            </a-radio>
            <a-radio :value="false">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div> </div
                  ><div className="custom-radio-card-title">驳回</div>
                </div>
              </template>
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item v-if="detail.identityCardImageJust" field="manual" label="身份证正面">
          <a-image
            class="img"
            :src="
              urla +
              '/app/file/download/private?path=' +
              encodeURIComponent(detail.identityCardImageJust)
            "
          />
        </a-form-item>
        <a-form-item v-if="detail.identityCardImageBack" label="身份证反面" field="remarks">
          <a-image
            class="img"
            :src="
              urla +
              '/app/file/download/private?path=' +
              encodeURIComponent(detail.identityCardImageBack)
            "
        /></a-form-item>
        <a-form-item v-if="detail.identityCardImageHand" label="手持身份证" field="remarks">
          <a-image
            class="img"
            :src="
              urla +
              '/app/file/download/private?path=' +
              encodeURIComponent(detail.identityCardImageHand)
            "
        /></a-form-item>
        <a-form-item v-if="detail.businessIcense" label="营业执照" field="remarks">
          <a-image
            class="img"
            :src="
              urla + '/app/file/download/private?path=' + encodeURIComponent(detail.businessIcense)
            "
        /></a-form-item>
        <a-form-item v-if="detail.other" label="其他材料" field="remarks">
          <a-image
            class="img"
            :src="urla + '/app/file/download/private?path=' + encodeURIComponent(detail.other)"
        /></a-form-item>
      </a-form>
      <template #footer>
        <a-space>
          <a-button @click="cancel">取消</a-button>
          <a-button type="primary" @click="ok">确定</a-button>
        </a-space>
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts">
  import * as http from '@/api/http'
  import { getUserRealName, clearUserReal, auditing, exportReal } from '@/api/url'
  import useCommonStore from '@/store/modules/common'
  import { Message, Modal } from '@arco-design/web-vue'
  import { usePagination, useRowKey, useTable, useTableHeight } from '@/hooks/table'
  import { defineComponent, getCurrentInstance, onMounted, reactive, ref } from 'vue'
  import { get } from '@/api/http'
  export default defineComponent({
    name: 'UserList',
    setup() {
      const useCommon = useCommonStore()
      const form = reactive({
        userId: '',
        name: '',
        phone: '',
        identityCard: '',
        addTime: '',
        endTime: '',
        state: '',
        type: '0',
        mode: '',
      })
      const urla = ref(window.location.protocol + '//' + window.location.host)
      const showV = ref(false)
      const loading = ref(false)
      const table = useTable()
      const rowKey = useRowKey('id')
      const pagination = usePagination(doRefresh)
      const userLists: any = ref([])
      const tableColumns: any = reactive({
        list: [
          {
            title: '用户名',
            key: 'userName',
            dataIndex: 'userName',
            ellipsis: true,
            tooltip: true,
            checked: true,
            width: 120,
          },
          {
            title: '客户姓名',
            key: 'name',
            dataIndex: 'name',
            checked: true,
            width: 120,
          },
          {
            title: '手机号',
            key: 'phone',
            dataIndex: 'phone',
            checked: true,
            width: 120,
          },
          {
            title: '身份证号',
            key: 'identityCard',
            dataIndex: 'identityCard',
            checked: true,
            width: 120,
          },
          {
            title: '实名方式',
            key: 'mode',
            dataIndex: 'mode',
            checked: true,
            width: 120,
          },
          {
            title: '实名状态',
            key: 'state',
            dataIndex: 'state',
            checked: true,
            width: 120,
          },
          {
            title: '实名时间',
            key: 'time',
            dataIndex: 'time',
            checked: true,
            width: 190,
          },
          {
            title: '操作',
            key: 'actions',
            dataIndex: 'actions',
            width: 150,
            checked: true,
          },
        ],
      })
      const ok = () => {
        get({
          url: auditing,
          data: {
            userId: detail.value.userId,
            success: detail.value.success,
          },
        }).then((res) => {
          if (res.code == 0) {
            doRefresh()
            Message.success(res.msg)
            cancel()
          }
        })
      }
      const showRef = ref()
      const cancel = () => {
        showRef.value?.resetFields()
        showV.value = false
      }
      const detail: any = ref({})
      const Real = (val: any) => {
        detail.value = JSON.parse(JSON.stringify(val))
        detail.value.success = true
        showV.value = true
      }
      const key = ref('0')
      const tableLoading = ref(true)
      const btnLoading = ref(false)
      const visible = ref(false)
      const exportLists = () => {
        loading.value = true
        useCommon
          .downLoads(exportReal, form, '客户实名列表')
          .then(() => {
            loading.value = false
          })
          .catch(() => {
            loading.value = false
          })
      }
      const tabClick = (val: any) => {
        form.type = val
        doRefresh(1)
      }
      function getUserList() {
        useCommon.userLists().then((res: any) => {
          userLists.value = res.data
        })
      }
      function doRefresh(value?: number) {
        // eslint-disable-next-line prettier/prettier
        tableLoading.value = true
        if (value) {
          pagination.page = value
        }
        http
          .get({
            url: getUserRealName,
            data: () => {
              return {
                page: pagination.page,
                size: pagination.pageSize,
                ...form,
              }
            },
          })
          .then((res) => {
            tableLoading.value = false
            table.handleSuccess(res.data.list)
            pagination.setTotalSize((res.data as any).totalSize)
          })
          .catch(console.log)
      }

      function clearReal(item: any) {
        Modal.warning({
          title: '操作确认',
          content: '确定要清除该客户实名信息吗？',
          cancelText: '取消',
          titleAlign: 'start',
          hideCancel: false,
          okText: '删除',
          onOk: () => {
            http
              .get({
                url: clearUserReal,
                data: { userId: item.userId },
              })
              .then((res) => {
                if (res.code == 0) {
                  Message.success(res.msg)
                  doRefresh()
                } else {
                  Message.error(res.msg)
                }
              })
              .catch(console.log)
          },
        })
      }
      function onUpdateBorder(isBordered: boolean) {
        table.bordered.value = isBordered
      }
      function onUpdateStriped(isStriped: boolean) {
        table.striped.value = isStriped
      }
      const serchForm = ref()
      const resetSeach = () => {
        serchForm.value?.resetFields()
        doRefresh(1)
      }
      onMounted(async () => {
        table.tableHeight.value = await useTableHeight(getCurrentInstance())
        doRefresh()
        getUserList()
      })
      return {
        showRef,
        Real,
        detail,
        ok,
        cancel,
        showV,
        tabClick,
        key,
        loading,
        visible,
        btnLoading,
        userLists,
        doRefresh,
        serchForm,
        resetSeach,
        tableLoading,
        form,
        ...table,
        rowKey,
        tableColumns,
        pagination,
        clearReal,
        exportLists,
        urla,
        onUpdateBorder,
        onUpdateStriped,
      }
    },
  })
</script>
<style lang="less" scoped>
  :deep(.arco-image-img) {
    height: 60px !important;
  }

  :deep(.arco-table-cell-expand-icon) {
    justify-content: center;
  }

  :deep(.arco-tabs-nav-tab) {
    background-color: var(--color-bg-1);
    padding-top: 15px;
    border-radius: 10px;
  }
  :deep(.arco-table-td-content button) {
    padding: 0px !important;
    margin-right: 8px;
    background: none !important;
  }
  :deep(.arco-tabs-content::before) {
    height: 0px !important;
  }

  :deep(.arco-tabs-tab) {
    padding: 0;
    padding-bottom: 5px;
  }

  :deep(.arco-tabs-nav::before) {
    position: static !important;
    height: 0px !important;
  }

  :deep(.arco-tabs-content) {
    padding-top: 20px;
  }
  :deep(.arco-modal) {
    .arco-modal-footer {
      text-align: right !important;
    }
  }
  :deep(.arco-modal-header .arco-modal-title) {
    display: flex;
    justify-content: flex-start !important;
  }
  .img {
    width: 60px;
    height: 60px;
  }
</style>
